package com.fire.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fire.mapper.LocationMapper;
import com.fire.mapper.MonitorMapper;
import com.fire.model.entity.Location;
import com.fire.model.entity.Now;
import com.fire.service.IAssessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: AssessServiceImpl
 * Package: com.fire.service.impl
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2024/4/6 - 21:52
 * @Version: v1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AssessServiceImpl implements IAssessService {

    private final MonitorMapper monitorMapper;

    private final LocationMapper locationMapper;

    @Value("${qweather.key}")
    public String key;

    private static String  URL = "https://api.qweather.com/v7/weather/7d?location=";
    private static String  AIR_URL = "https://api.qweather.com/v7/air/now?location=";

    @Override
    public Map<String, Object> queryAssessData() {
        long begin = System.currentTimeMillis();
        Map<String, String> locationMap = this.getLocationId();
        String locationId = locationMap.get("locationId");
        String locationName = locationMap.get("locationName");
        log.info("开始查询{}城市的未来一周气温变化....", locationName);
        String jsonStr = HttpUtil.get(URL + locationId + "&key=" + key);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String code = jsonObject.getString("code");
        if(code.equalsIgnoreCase("200")){
            List<Integer> highest = new ArrayList<>();
            List<String> dateTime = new ArrayList<>();
            List<Integer> lowest = new ArrayList<>();
            JSONArray jsonArray = jsonObject.getJSONArray("daily");
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                highest.add(Integer.parseInt(jsonObject1.getString("tempMax")));
                lowest.add(Integer.parseInt(jsonObject1.getString("tempMin")));
                dateTime.add(jsonObject1.getString("fxDate"));
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("highest",highest);
            map.put("lowest",lowest);
            map.put("dateTime",dateTime);
            map.put("locationName", locationName);
            long end = System.currentTimeMillis();
            log.info("结束查询{}城市的未来一周气温变化...耗时：{}", locationName,(end-begin));
            return map;
        }
        return null;
    }

    @Override
    public Now queryAirData() {
        long begin = System.currentTimeMillis();
        Map<String, String> locationMap = this.getLocationId();
        String locationId = locationMap.get("locationId");
        String locationName = locationMap.get("locationName");
        log.info("开始查询{}城市的实时空气质量变化....", locationName);
        String jsonStr = HttpUtil.get(AIR_URL + locationId + "&key=" + key);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String code = jsonObject.getString("code");
        if(code.equalsIgnoreCase("200")){
            Now now = jsonObject.getJSONObject("now").toJavaObject(Now.class);
            now.setLocationName(locationName);
            long end = System.currentTimeMillis();
            log.info("结束查询{}城市的未来一周气温变化...耗时：{}", locationName,(end-begin));
            return now;
        }
        return null;
    }

    private Map<String,String> getLocationId() {
        String locationName = monitorMapper.queryLocationName();
        QueryWrapper<Location> wrapper = new QueryWrapper<>();
        wrapper.eq("location_name", locationName);
        Location location = locationMapper.selectOne(wrapper);
        Map<String,String> map = new HashMap<>();
        map.put("locationId",location.getLocationId());
        map.put("locationName",locationName);
        return map;
    }
}
